package com.ithe.redis.redission;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by zxd on 2022/5/7
 */
@Configuration
@Slf4j
public class RedissionConfig {

        @Autowired
        private RedisProperties redisProperties;


        @Bean
        public RedissonClient redissonClient(){
            RedissonClient redissonClient;
            Config config = new Config();
            String url = "redis://" + redisProperties.getHost()
                    + ":" + redisProperties.getPort();
            config.useSingleServer().setAddress(url) //单机
                    .setPassword(redisProperties.getPassword());
            //添加主从配置
            //config.useMasterSlaveServers().setMasterAddress("").setPassword("").addSlaveAddress(new String[]{"",""});
            //集群
            //config.useClusterServers().addNodeAddress(new String[]{"",""}).setPassword("");
            try {
                redissonClient = Redisson.create(config);
                return redissonClient;
            } catch (Exception e) {
                log.error("RedissonClient init redis url :[{}]",url,e.getMessage());
                return null;
            }
        }

        @Bean
        public ResissonUtil resissonUtil(RedissonClient redissonClient){
            ResissonUtil resissonUtil=new ResissonUtil();
            resissonUtil.setRedissonClient(redissonClient);
            return resissonUtil;
        }

        @Bean
        public RedissionLockUtil redissionLockUtil(RedissonClient redissonClient){
            RedissionLockUtil redissionLockUtil=new RedissionLockUtil();
            redissionLockUtil.setRedissonClient(redissonClient);
            return redissionLockUtil;
        }






}
